package com.bw.crop.domain;

import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONObject;
import com.bw.crop.service.DyCropsService;
import com.bw.crop.service.SysCropsService;
import com.bw.crop.units.Result;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;
import java.util.concurrent.CompletableFuture;
import java.util.stream.Collectors;

@Component
public class DataSourceLoader {
    @Autowired
    private   SysCropsService sysCropsService;
    @Autowired
    private   DyCropsService dyCropsService;
    @Autowired
    private RedisTemplate redisTemplate;

    // 模拟从数据源A加载数据
    public CompletableFuture<Result> loadDataFromSourceA(Integer size) {

        return CompletableFuture.supplyAsync(() -> {
            // 模拟长时间的数据加载过程
            PageHelper.startPage(1,size-1);
            List<SysCrops> list;
            try {
                if(redisTemplate.hasKey("firstlist")){
                    List<Object> firstlist = redisTemplate.opsForList().range("firstlist", 0, -1);
                    List<SysCrops> collect = firstlist.stream()
                            .map(jsonObject -> JSON.toJavaObject(jsonObject, SysCrops.class))
                            .collect(Collectors.toList());
                    return Result.success(new PageInfo<>(collect).getList());
                }
                list= sysCropsService.list();
                redisTemplate.opsForList().leftPushAll("firstlist",list);

            } catch (Exception e) {
                throw new IllegalStateException(e);
            }
            return Result.success(new PageInfo<>(list).getList());
        });
    }

    // 模拟从数据源B加载数据
    public  CompletableFuture<Result> loadDataFromSourceB(Integer size) {
        return CompletableFuture.supplyAsync(() -> {
            // 模拟另一个长时间的数据加载过程
            PageHelper.startPage(1,size);
            List<DyCrops> list;
            DyCrops dyCrops=null;
            try {
                if(redisTemplate.hasKey("dfirstlist")){
                    List<Object> dfirstlist = redisTemplate.opsForList().range("dfirstlist", 0, -1);
                    List<DyCrops> collect = dfirstlist.stream()
                            .map(jsonObject -> JSON.toJavaObject(jsonObject, DyCrops.class))
                            .collect(Collectors.toList());
                    return Result.success(new PageInfo<>(collect).getList());
                }
                list = dyCropsService.list();
                dyCrops = list.get(size-1);
                redisTemplate.opsForList().leftPushAll("dfirstlist",Arrays.asList(dyCrops));

            } catch (Exception e) {
                throw new IllegalStateException(e);
            }
            return Result.success(new PageInfo<>(Arrays.asList(dyCrops)).getList());
        });
    }

    // 可以继续为其他数据源定义类似的方法...
}